package cn.modoumama.common.tools;

import org.apache.commons.beanutils.BeanComparator;
import org.apache.commons.collections.ComparatorUtils;
import org.apache.commons.collections.comparators.ComparableComparator;
import org.apache.commons.collections.comparators.ComparatorChain;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @Description 排序
 * @Author dengqiang
 * @Date 2019/1/25 19:09
 * @Version 1.0
 **/
public class SortUtils {

    /**
     * list排序
     * @param sortList
     * @param param1
     * @param orderType
     * @param <T>
     * @return
     */
    public static <T> List<T> sort(List<T> sortList, String param1, String orderType) {
        Comparator<Comparator> mycmp = ComparableComparator.getInstance();
        if ("desc".equals(orderType)) {
            mycmp = ComparatorUtils.reversedComparator(mycmp);
        }

        ArrayList<Object> sortFields = new ArrayList();
        sortFields.add(new BeanComparator(param1, mycmp));
        ComparatorChain multiSort = new ComparatorChain(sortFields);
        Collections.sort(sortList, multiSort);
        return sortList;
    }
}
